package com.jdl.iscm.advise;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.stream.IntStream;

/**
 * @Author: Ran
 * @Date: 2021/8/17
 **/
@Aspect
@Component
@Slf4j
public class SleepAdvise {

    /**
     * 定义切点
     */
    @Pointcut("@annotation(com.jdl.iscm.advise.Sleep)")
    private void pointCut() {
    }

    /**
     * 添加环绕日志及休眠
     *
     * @param joinPoint
     * @throws Throwable
     */
    @Around("pointCut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        //1、进入方法记录日志
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        Object[] objects = joinPoint.getArgs();
        log.info("开始执行 ======> {}", method.getName());
        IntStream.range(0, objects.length).filter(i -> i != 0).forEach(i -> log.info("--请求入参 {}", objects[i].toString()));
        //2、执行业务方法
        joinPoint.proceed();
        //3、强制休眠，增强稳定性，默认1秒
        Thread.sleep((method.getAnnotation(Sleep.class).sleep()));
    }
}


